window.define(["jquery"], function ($) {
    var basePath = "/api";
    var basePathTest = "assets/mock";
    var serversCommon={
        getReqOption:function(params){
            var extParam=$.ExtAssist.urlParam.getParam();
            var option={
                url:params[0],
                param:params[1]||{},
                successCallback:params[2],
                isLoading:params[3],
                token:null
            };
            option.param=$.ExtCommon.initMergeJson(extParam, option.param);
            return option;
        },
        getMapInfo:function(type,id){
            var strId=id.toString();
            return dataMapping[type].hasOwnProperty(strId)?dataMapping[type][strId]:{idMap:strId}
        },
        getMapKey:function(type,id){
            var idKey="";
            var strId=(typeof id==="undefined" || id===null)?"":id.toString();
            if(strId.length>0){
                var json=dataMapping[type];
                for(var k in json){
                    if(json.hasOwnProperty(k)){
                        if(json[k].idMap===strId){
                            idKey= k.toString();
                            break;
                        }
                    }
                }
            }
            return idKey.length===0?strId:idKey;
        },
        getListMap:function(res,type){
            var list=res.success?res.data:[];
            for(var i=0;i<list.length;i++){
                var info =list[i];
                var id=info.id;
                var mInfo=this.getMapInfo(type,id);
                info.idMap=mInfo.idMap;
                info.mapInfo=mInfo;
            }
            return list;
        }
    };
    var dataMapping={
        crowdType:{"CUSTOMER_FILE":{idMap:"1",name:"自定义人群"},"LOOKALIKE":{idMap:"2",name:"相似人群"}},
        customerStatus:{"-1":{idMap:"-1",name:"审核不通过"},"0":{idMap:"0",name:"待提交"},"1":{idMap:"1",name:"待审核"},"2":{idMap:"2",name:"审核通过"},"3":{idMap:"3",name:"冻结"}},
        spreadType:{"1":{idMap:"1"},"2":{idMap:"2"}},
        objMatterType:{"1":{idMap:"1",filterType:"LINK"},"2":{idMap:"2",filterType:"ANDROID"},"3":{idMap:"3",filterType:"IOS"},"4":{idMap:"4",filterType:"WECHAT"}}
    };
    var servers = {
        getPageParamMap:function(){
            return {offset:"offset",limit:"limit",search:"search"}
        },
        getSendSuccess:function (data) {
            return {success:true,data:data,dataOriginal:data}
        },
        getSendError:function (msg) {
            return {success:false,msg:msg}
        }
    };
    servers.base={
        industry: function (cb) {
            var options=serversCommon.getReqOption([basePath + "/common/customer/industry", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        crowdBag: function (cb) {
            var options=serversCommon.getReqOption([basePath + "/audience/numberType", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        spreadType: function (cb) {
            var options=serversCommon.getReqOption([basePath + "/1/promotionPlan/promotionPlanType", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        objMatterType: function (cb) {
            var options=serversCommon.getReqOption([basePath + "/creative/product/list", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        channelPack: function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/creative/gdt/product/list", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        resourceType: function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/common/platformList",param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        getMapCrowdType:function (key,isMap) {
            isMap=isMap||false;
            if(isMap){
                return serversCommon.getMapKey("crowdType",key);
            }else{
                return serversCommon.getMapInfo("crowdType",key);
            }
        },
        getMapCustomerStatus:function (key,isMap) {
            isMap=isMap||false;
            if(isMap){
                return serversCommon.getMapKey("customerStatus",key);
            }else{
                return serversCommon.getMapInfo("customerStatus",key);
            }
        },
        getMapSpreadType:function (key,isMap) {
            isMap=isMap||false;
            if(isMap){
                return serversCommon.getMapKey("spreadType",key);
            }else{
                return serversCommon.getMapInfo("spreadType",key);
            }
        },
        getMapObjMatterType:function (key,isMap) {
            isMap=isMap||false;
            if(isMap){
                return serversCommon.getMapKey("objMatterType",key);
            }else{
                return serversCommon.getMapInfo("objMatterType",key);
            }
        },
        uploadImg:basePath + "/common/upload/image",
        uploadFile:basePath + "/audience/fileUpload"
    };
    servers.account = {
        dataInfo:null,
        //dataToken:null,
        info:function(cb){
            var self=this;
            if(!$.ExtCommon.isCheckEmpty(self.dataInfo)){
                cb(self.dataInfo)
            }else{
                var options=serversCommon.getReqOption([basePathTest + "/account/info.json", {}, function(res){
                //var options=serversCommon.getReqOption([basePath + "/user/login", {}, function(res){
                    var newJson={};
                    if(res.success){
                        newJson= $.ExtCommon.getMappingJson(res.data, {
                            "userId":"userId","userName":"name","authorize":"permissions","roleName":"role"
                        },true);
                        $.cookie('usertoken',"1");
                    }
                    res.data=newJson;
                    self.dataInfo=res;
                    cb(self.dataInfo);
                }]);
                $.ExtAjax.initGetSimple(options);
            }
        },
        login: function (param, cb) {
            var self=this;
            var options=serversCommon.getReqOption(["/auth", param, function(res){
                if(res.success){
                    self.dataInfo=null;
                    self.info(function(resInfo){
                        cb(resInfo)
                    });
                }else{
                    cb(res)
                }
            },false]);
            $.ExtAjax.initPostSimple(options);
        },
        logout: function (cb) {
            var self=this;
            var options=serversCommon.getReqOption(["/logout", {}, function(res){
                if(res.success){
                    self.dataToken=null;
                    self.dataInfo=null;
                    $.cookie('usertoken',false);
                }
                cb(res)
            }]);
            $.ExtAjax.initPostSimple(options);
        },
        check: function (cb) {
            this.info(function(resInfo){
                cb(resInfo)
            });
        },
        pwd: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/user/password", data, cb]);
            $.ExtAjax.initPutSimple(options);
        },
        pagePower:function(param,cb){
            //var options=serversCommon.getReqOption([basePath + "/user/hasPermission", param, cb]);
            var options=serversCommon.getReqOption([basePathTest + "/account/permission.json", param, cb]);
            $.ExtAjax.initGetSimple(options);
        }
    };
    servers.customer={
        pageList: function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/customer", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        list:function (cb) {
            var options=serversCommon.getReqOption([basePath + "/customer/dropList", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        info: function (id, cb) {
            var options=serversCommon.getReqOption([basePath + "/customer/"+id, {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        add: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/customer", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        update: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/customer", data, cb]);
            $.ExtAjax.initPutSimple(options);
        }
    };
    servers.campaign={
        pageList: function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/campaign", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        info: function (id, cb) {
            var options=serversCommon.getReqOption([basePath + "/campaign/"+id, {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        add: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/campaign", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        update: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/campaign", data, cb]);
            $.ExtAjax.initPutSimple(options);
        }
    };
    servers.spread={
        pageList: function (campaignId,param,cb) {
            var url = $.ExtCommon.getStringConnect("/{0}/promotionPlan/selectPromotionPlan", [campaignId]);
            var options=serversCommon.getReqOption([basePath + url, param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        list:function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/common/promotionPlanStruct", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        info: function (campaignId,param, cb) {
            var url = $.ExtCommon.getStringConnect("/{0}/promotionPlan/getPromotionPlan", [campaignId]);
            var options=serversCommon.getReqOption([basePath + url, param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        add: function (campaignId,data, cb) {
            var url = $.ExtCommon.getStringConnect("/{0}/promotionPlan/addPromotionPlan", [campaignId]);
            var options=serversCommon.getReqOption([basePath + url, data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        update: function (campaignId,data, cb) {
            var url = $.ExtCommon.getStringConnect("/{0}/promotionPlan/updatePromotionPlan", [campaignId]);
            var options=serversCommon.getReqOption([basePath + url, data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        updateStatus: function (campaignId,data, cb) {
            var url = $.ExtCommon.getStringConnect("/{0}/promotionPlan/updateStatus", [campaignId]);
            var options=serversCommon.getReqOption([basePath + url, data, cb,false]);
            $.ExtAjax.initPostSimple(options);
        },
        updatePrice: function (campaignId,data, cb) {
            var url = $.ExtCommon.getStringConnect("/{0}/promotionPlan/updateDayBudget", [campaignId]);
            var options=serversCommon.getReqOption([basePath + url, data, cb,false]);
            $.ExtAjax.initPostSimple(options);
        }
    };
    servers.strategy= {
        pageList: function (campaignId, param, cb) {
            var url = $.ExtCommon.getStringConnect("/{0}/strategy", [campaignId]);
            var options = serversCommon.getReqOption([basePath + url, param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        info: function (param, cb) {
            var url = $.ExtCommon.getStringConnect("/{0}/strategy/{1}", [param.campaignId, param.id]);
            var options = serversCommon.getReqOption([basePath + url, {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        add: function (campaignId,data, cb) {
            var url = $.ExtCommon.getStringConnect("/{0}/strategy", [campaignId]);
            var options = serversCommon.getReqOption([basePath + url, data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        update: function (campaignId,data, cb) {
            var url = $.ExtCommon.getStringConnect("/{0}/strategy", [campaignId]);
            var options = serversCommon.getReqOption([basePath + url, data, cb]);
            $.ExtAjax.initPutSimple(options);
        },
        updateStatus: function (campaignId, data, cb) {
            var url = $.ExtCommon.getStringConnect("/{0}/strategy/status", [campaignId]);
            var options = serversCommon.getReqOption([basePath+url, data, cb, false]);
            $.ExtAjax.initPutSimple(options);
        },
        updatePrice: function (campaignId, data, cb) {
            var url = $.ExtCommon.getStringConnect("/{0}/strategy/price", [campaignId]);
            var options = serversCommon.getReqOption([basePath + url, data, cb, false]);
            $.ExtAjax.initPutSimple(options);
        }
    };
    servers.creative={
        pageList: function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/creative/list", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        modalList: function (campaignId,param,cb) {
            var url = $.ExtCommon.getStringConnect("/{0}/strategy/creative", [campaignId]);
            var options=serversCommon.getReqOption([basePath + url, param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        info: function (id, cb) {
            var options=serversCommon.getReqOption([basePath + "/creative/get/"+id, {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        add: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/creative/add", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        update: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/creative/update", data, cb]);
            $.ExtAjax.initPutSimple(options);
        },
        updateStatus: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/creative/status", data, cb,false]);
            $.ExtAjax.initPutSimple(options);
        }
    };
    servers.media={
        list:function (cb) {
            var options=serversCommon.getReqOption([basePath + "/common/mediasStruct", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        }
    };
    servers.space={
        modalList: function (campaignId,param,cb) {
            var url = $.ExtCommon.getStringConnect("/{0}/strategy/space", [campaignId]);
            var options=serversCommon.getReqOption([basePath + url, param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        materialInfo:function (id,cb) {
            var options=serversCommon.getReqOption([basePath + "/creative/templateSpec/"+id, {}, cb]);
            $.ExtAjax.initGetSimple(options);
        }
    };
    servers.targetPack={
        pageList: function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/targetPackage", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        list:function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/common/targetPackageStruct", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        strategyList:function(id,cb) {
            var options=serversCommon.getReqOption([basePath + "/targetPackage/"+id+"/strategy/list", {}, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        info: function (id, cb) {
            var options=serversCommon.getReqOption([basePath + "/targetPackage/"+id, {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        add: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/targetPackage", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        update: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/targetPackage", data, cb]);
            $.ExtAjax.initPutSimple(options);
        },
        delete: function (id, cb) {
            var options=serversCommon.getReqOption([basePath + "/targetPackage/"+id, {}, cb]);
            $.ExtAjax.initDeleteSimple(options);
        }
    };
    servers.targets={
        area:function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/common/regionStruct", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        interest:function (cb) {
            var options=serversCommon.getReqOption([basePath + "/common/businessInterestStruct", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        crowd:function (cb) {
            var options=serversCommon.getReqOption([basePath + "/common/personals", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        device:function (cb) {
            var options=serversCommon.getReqOption([basePath + "/common/mobileDeviceStruct", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        appAction:function (cb) {
            var options=serversCommon.getReqOption([basePath + "/common/appStruct", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        }
    };
    servers.crowd={
        pageList: function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/audience/listAudience", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        customList: function (cb) {
            var options=serversCommon.getReqOption([basePath + "/audience/audienceFileList", {}, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        customInfo: function (param, cb) {
            var options=serversCommon.getReqOption([basePath + "/audience/getAudienceFile", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        addCustom: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/audience/addAudience", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        updateCustom: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/audience/updateAudience", data, cb]);
            $.ExtAjax.initPutSimple(options);
        },
        delete: function (param, cb) {
            var options=serversCommon.getReqOption([basePath + "/audience/removeAudience", param, cb]);
            $.ExtAjax.initPutSimple(options);
        },
        similarInfo: function (param, cb) {
            var options=serversCommon.getReqOption([basePath + "/audience/getAudienceLike", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        addSimilar: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/audienceLike/addAudienceLike", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        updateSimilar: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/audience/updateAudience", data, cb]);
            $.ExtAjax.initPutSimple(options);
        }
    };
    servers.report={
        pageList: function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/report", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        reportCity: function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/report/state", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        reportGender: function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/report/gender", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        reportAge:function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/report/age", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        campaignHour: function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/report/campaign/hourly", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        campaignDay: function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/report/campaign/daily", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        strategyHour: function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/report/strategy/hourly", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        strategyDay: function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/report/strategy/daily", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        //down
        downHour:function(param){
            window.location.href=basePathTest + "/report/info.json?"+param;
        },
        downDay:function(param){
            window.location.href=basePathTest + "/report/info.json?"+param;
        },
        downloadStrategyGroup:function(param){
            window.location.href=basePathTest + "/report/info.json?"+param;
        },
        downloadStrategy:function(param){
            window.location.href=basePathTest + "/report/info.json?"+param;
        },
        downloadCity:function(param){
            window.location.href=basePathTest + "/report/info.json?"+param;
        },
        downloadGender:function(param){
            window.location.href=basePathTest + "/report/info.json?"+param;
        },
        downloadAge:function(param){
            window.location.href=basePathTest + "/report/info.json?"+param;
        }
    };
    //用户
    servers.user={
        pageList: function (param,cb) {
            var options=serversCommon.getReqOption([basePath + "/user", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        add: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/user", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        update: function (data, cb) {
            var options=serversCommon.getReqOption([basePath + "/user", data, cb]);
            $.ExtAjax.initPutSimple(options);
        }
    };
    servers.finance={
        agentListTransfer: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/AgentListTransfer.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        agentTransferIn: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        agentTransferOut: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        agentListStream: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/AgentListStream.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        customerListTransfer: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/CustomerListTransfer.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        customerListConsume: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/CustomerListConsume.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        surplusAmount: function (cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/Info.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        surplusAmountByCustomer: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/Info1.json", param, cb]);
            $.ExtAjax.initGetSimple(options);
        }
    };
    return servers;
});